﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Broker.View.Comm;
using NHibernate;
using Broker.Model;
using BrokerBase.Lib.Company;
using BrokerBase.Lib.Client;
using Broker.Update;
using System.Windows.Controls;
using log4net;
using BrokerBase.Lib.Product;
using System.ServiceModel;
using Broker.DataService;

namespace Broker
{
    public static class AppGlobal
    {
        private static object syncClientCfgRoot = new object();
        private static ClientConfiguration clientCfg;

        private static object syncClientDataInfoRoot = new object();
        private static ClientDataInfo clientDataInfo=new ClientDataInfo();

        private const int MAX_LENGTH = 256 * 1024 * 1024;//256M的最大数据

        private static readonly BaseDataType[] dts = new BaseDataType[]
            {
                new BaseDataType("int","整数"),
                new BaseDataType("double","小数"),
                new BaseDataType("string","文本"),
                new BaseDataType("bool","布尔"),
                new BaseDataType("DateTime","日期")
            };

        public static ISessionFactory HbmSessionFactory { get; set; }

        public static ContentControl MainWin { get; set; }

        public static ClientConfiguration ClientCfg 
        {
            get
            {
                lock (syncClientCfgRoot)
                {
                    return clientCfg;
                }
            }
            set
            {
                lock (syncClientCfgRoot)
                {
                    clientCfg = value;
                }
            }
        }

        public static ClientDataInfo ResultClientRegInfo
        {
            get
            {
                lock (syncClientDataInfoRoot)
                {
                    return clientDataInfo;
                }
            }
            set
            {
                lock (syncClientDataInfoRoot)
                {
                    clientDataInfo = value;
                }
            }
        }



        public static ICompany CurrentCompany { get; set; }

        public static BaseDataType[] BaseDataType
        {
            get { return dts; }
        }

        public  static  SysUser CurrentUser { get; set; }


        public static ILog Log { get; set; }

        public static IEdition Edition { get; set; }


        public static IProduct LoadTrialProductId(ICompany company)
        {
            ISession sess = HbmSessionFactory.GetCurrentSession();

            IProduct p = sess.CreateQuery("from Product as p where p.Company.Id = " + company.Id + " and p.Id=(select Min(Id) from Product as p2 where p2.Company.Id="+company.Id+")").UniqueResult<IProduct>();

            return p;
            
        }

        public static string GetAppPath()
        {
            return System.Environment.CurrentDirectory;
        }

        //public static string GetAppDataPath()
        //{
        //    return System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Milliondone";
        //}

        public static RemoteDataService DataWCF { get; set; }

        public static ChannelFactory<IDataService> CreateDataServiceFactory()
        {
            BasicHttpBinding bind = CreateBasicBind();
            EndpointAddress endPoint = new EndpointAddress(AppGlobal.ClientCfg.DataServiceUrl);

            ChannelFactory<IDataService> channelFactory = new ChannelFactory<IDataService>(bind, endPoint);

            return channelFactory;

        }

        //public static ChannelFactory<ILicenseService> CreateLicenseChannelFactory()
        //{

        //    WSHttpBinding bind = CreateBind();

        //    EndpointAddress ead = new EndpointAddress(AppGlobal.ClientCfg.LicenseUrl);

        //    ChannelFactory<ILicenseService> channelFacotry = new ChannelFactory<ILicenseService>(bind, ead);

        //    return channelFacotry;
        //}

        //public static ChannelFactory<IDataUpdateService> CreateDataUpdateChannelFactory()
        //{
        //    WSHttpBinding bind = CreateBind();

        //    EndpointAddress ead = new EndpointAddress(AppGlobal.ClientCfg.DataUpdateUrl);

        //    ChannelFactory<IDataUpdateService> channelFacotry = new ChannelFactory<IDataUpdateService>(bind, ead);

        //    return channelFacotry;
        //}

        private static BasicHttpBinding CreateBasicBind()
        {
            BasicHttpBinding bind = new BasicHttpBinding();
            bind.ReaderQuotas.MaxArrayLength = MAX_LENGTH;
            bind.ReaderQuotas.MaxBytesPerRead = MAX_LENGTH;
            bind.ReaderQuotas.MaxDepth = 64;
            bind.MaxBufferPoolSize = MAX_LENGTH;
            bind.MaxReceivedMessageSize = MAX_LENGTH;

            bind.SendTimeout = new TimeSpan(0, 5, 0);
            bind.ReceiveTimeout = new TimeSpan(0, 5, 0);

            return bind;
        }

        private static WSHttpBinding CreateBind()
        {
            WSHttpBinding bind = new WSHttpBinding();
            bind.Security.Mode = SecurityMode.None;
            bind.Security.Message.ClientCredentialType = MessageCredentialType.None;
            bind.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            bind.ReliableSession.Enabled = true;
            bind.Security.Message.EstablishSecurityContext = false;

            bind.ReaderQuotas.MaxArrayLength = MAX_LENGTH;//设置最大可接受的数组
            bind.ReaderQuotas.MaxBytesPerRead = MAX_LENGTH;
            bind.ReaderQuotas.MaxStringContentLength = MAX_LENGTH;
            bind.ReaderQuotas.MaxDepth = 64;

            bind.MaxBufferPoolSize = MAX_LENGTH;
            bind.MaxReceivedMessageSize = MAX_LENGTH;

            bind.SendTimeout = new TimeSpan(0, 5, 0);
            bind.ReceiveTimeout = new TimeSpan(0, 10, 0);
            
            return bind;
        }

    }
}
